//
//  RegisterVC.m
//  AlumniBook
//
//  Created by 喂！ on 2018/10/24.
//  Copyright © 2018年 well. All rights reserved.
//

#import "RegisterVC.h"

@interface RegisterVC ()

@end

@implementation RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initFrame];
    self.view.backgroundColor = [UIColor whiteColor];
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:true];
}

-(void)initFrame
{
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"注册";
    nameLabel.font = [UIFont systemFontOfSize:64 * SCREEN_SCALE];
    nameLabel.textColor = [UIColor colorWithRed:23/255.0f green:111/255.0f blue:185/255.0f alpha:1.0f];
    [self.view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_top).offset(300 * SCREEN_SCALE);
    }];
    
    
    
    
    
    UIView *accountView = [[UIView alloc] init];
    accountView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:accountView];
    [accountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_bottom).multipliedBy(655/1334.0f);
        make.width.mas_equalTo(self.view).multipliedBy(454/750.0f);
        make.height.mas_equalTo(accountView.mas_width).multipliedBy(67/454.0f);
    }];
    
    UIImageView *accountIcon = [[UIImageView alloc] init];
    [accountIcon  setContentHuggingPriority:UILayoutPriorityRequired
                                    forAxis:UILayoutConstraintAxisHorizontal];
    accountIcon.image = [UIImage imageNamed:@"Login_Account"];
    [accountView addSubview:accountIcon];
    [accountIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(accountView);
    }];
    
    
    self.accountField.font = [UIFont systemFontOfSize:30 * SCREEN_SCALE];
    self.accountField.textColor = [UIColor colorWithRed:58/255.0f green:59/255.0f blue:59/255.0f alpha:1.0f];
    self.accountField.autocorrectionType = UITextAutocorrectionTypeNo;
    self.accountField.returnKeyType = UIReturnKeyDone;
    self.accountField.delegate = self;
    [accountView addSubview:self.accountField];
    [self.accountField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(accountView);
        make.top.bottom.mas_equalTo(accountIcon);
        make.left.mas_equalTo(accountIcon.mas_right).offset(12);
    }];
    
    UIView *accountLine = [[UIView alloc] init];
    accountLine.backgroundColor = [UIColor colorWithRed:154/255.0f green:154/255.0f blue:154/255.0f alpha:1.0f];
    [accountView addSubview:accountLine];
    [accountLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.accountField);
        make.bottom.mas_equalTo(accountView);
        make.height.mas_equalTo(0.5);
    }];
    
    UIView *passwordView = [[UIView alloc] init];
    passwordView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:passwordView];
    [passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(accountView);
        make.top.mas_equalTo(accountView.mas_bottom).offset(20 * SCREEN_SCALE);
    }];
    
    UIImageView *passwordIcon = [[UIImageView alloc] init];
    passwordIcon.image = [UIImage imageNamed:@"Login_Password"];
    [passwordIcon  setContentHuggingPriority:UILayoutPriorityRequired
                                     forAxis:UILayoutConstraintAxisHorizontal];
    [passwordView addSubview:passwordIcon];
    [passwordIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(passwordView);
    }];
    
    
    self.passwordField.font = [UIFont systemFontOfSize:30 * SCREEN_SCALE];
    self.passwordField.secureTextEntry = true;
    self.passwordField.returnKeyType = UIReturnKeyDone;
    self.passwordField.textColor = [UIColor colorWithRed:58/255.0f green:59/255.0f blue:59/255.0f alpha:1.0f];
    self.passwordField.delegate = self;
    [passwordView addSubview:self.passwordField];
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(passwordView);
        make.top.bottom.mas_equalTo(passwordIcon);
        make.left.mas_equalTo(self.accountField);
    }];
    
    UIView *passwordLine = [[UIView alloc] init];
    passwordLine.backgroundColor = [UIColor colorWithRed:169/255.0f green:169/255.0f blue:169/255.0f alpha:1.0f];
    [passwordView addSubview:passwordLine];
    [passwordLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.passwordField);
        make.bottom.mas_equalTo(passwordView);
        make.height.mas_equalTo(0.5);
    }];
    

    
    [self.registerButton addTarget:self action:@selector(registerClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
    self.registerButton.backgroundColor = DefalutBlue;
    [self.view addSubview:self.registerButton];
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(passwordLine.mas_bottom).offset(48 * SCREEN_SCALE);
        make.width.mas_equalTo(300 * SCREEN_SCALE);
    }];
}



- (void)registerClick:(id)sender
{
        NSString *accountStr = self.accountField.text;
        NSString *passwordStr = self.passwordField.text;
    
        //登录前本地判断用户名账号
        if ([accountStr isEqualToString:@""] || [passwordStr isEqualToString:@""]) {
            [self loginFailed:@"用户名或密码不能为空"];
            return;
        }
    
        loginAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请求中" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:loginAlert animated:false completion:^{
            NSMutableDictionary *loginDic = [[NSMutableDictionary alloc] init];
            [loginDic setValue:accountStr forKey:@"name"];
            [loginDic setValue:passwordStr forKey:@"password"];
            MoRegister *moRegister = [[MoRegister alloc] init];
            [moRegister requestToService:loginDic];
            moRegister.baseHttpServiceBlock = ^(NSMutableDictionary * dic)
            {
                [self loginSuccess:dic];
            };
        }];
    
}

-(void)loginSuccess:(NSMutableDictionary *)dic
{
        [loginAlert dismissViewControllerAnimated:false completion:^{
            NSInteger status = [[dic valueForKey:@"Result"] integerValue];

            if (status == 0) {
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册成功" message:@"请重新登录" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:NO completion:nil];
                }];
                                         
                [alert addAction:action];
                [self presentViewController:alert animated:false completion:nil];


            }
            else
            {
                [self loginFailed:[dic valueForKey:@"ErrMsg"]];//登录失败处理
            }
        }];
}


-(void)loginFailed:(NSString *)str
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册失败" message:str preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:false completion:nil];
}



#pragma mark - UITextField
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.accountField) {
        [self.accountField resignFirstResponder];
        [self.passwordField becomeFirstResponder];
    }
    else
    {
        [self.passwordField resignFirstResponder];
        [self registerClick:nil];
    }
    
    return true;
}


#pragma mark    懒加载

-(UIButton *)registerButton
{
    if (!_registerButton) {
        _registerButton = [UIButton new];
    }
    return _registerButton;
}

-(UITextField *)accountField
{
    if (!_accountField) {
        _accountField = [UITextField new];
    }
    return _accountField;
}

-(UITextField *)passwordField
{
    if (!_passwordField) {
        _passwordField = [UITextField new];
    }
    return _passwordField;
}




@end
